/**
 * Copyright 2012 Roger Cockshutt
 * krakenbrau@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.parallelrl.util;

/**
 * @author Roger Cockshutt
 *
 */
public class Box2D {

	public Point2D center;
	public Point2D halfDimension;
	
	public Box2D(Point2D cntr, Point2D halfDim) {
		center = cntr;
		halfDimension = halfDim;
	}
	
	public Box2D(Integer ySize, Integer xSize) {
		center = new Point2D(ySize / 2, xSize / 2);
		halfDimension = new Point2D(ySize / 2, xSize / 2);
	}
	
	public Boolean containsPoint(Point2D p) {
		Boolean contains = false;
		if (p.y >= center.y - halfDimension.y
				&& p.y < center.y + halfDimension.y
				&& p.x >= center.x - halfDimension.x
				&& p.x < center.x + halfDimension.x) {
			contains = true;
		}
		return contains;
	}
	
	public Boolean intersectsBox(Box2D b) {
		Boolean intersects = false;
		Integer maxYIntersectDistance = halfDimension.y + b.halfDimension.y;
		Integer maxXIntersectDistance = halfDimension.x + b.halfDimension.x;
		Integer yDistance = Math.abs(center.y - b.center.y);
		Integer xDistance = Math.abs(center.x - b.center.x);
		if (yDistance < maxYIntersectDistance
				|| xDistance < maxXIntersectDistance) {
			intersects = true;
		}
		return intersects;
	}

}
